<?php
namespace app\modules\mch\models\purchase;
use app\models\PurchaseApply;
use app\models\Purchase;
use app\modules\mch\models\MchModel;
use app\models\User;
use yii\data\Pagination;

class PurchaseApplyForm extends MchModel
{
    public $store_id;
    public $model;
	public $status;
	public $user_id;
    public $realname;
    public $tel;
    public $name;
    public $address;

    public $review_result;


    public function rules()
    {
        return [
            [['store_id','user_id','status'], 'integer'],
			[['realname', 'tel', 'name', 'address'], 'required'],
            [['realname', 'tel', 'name', 'address'], 'string','max'=>250],
            [['user_id'], 'integer'],
		    [['review_result'], 'string'],
            [['status'], 'in', 'range' => [1, 2]],
        ];
    }

    public function attributeLabels() 
    { 
        return [ 
            'id' => 'ID',
			'status'=>'状态',
            'store_id' => 'Store ID',
            'user_id' => '用户',
			'name'=>'团队名称',
			'realname' => '联系人',
            'tel' => '联系电话',
            'address' => '详细地址',
			'review_result'=>'详细信息',
           
        ]; 
    }

    public function search()
    {
		 
        $query = PurchaseApply::find()
            ->where([
                'store_id' => $this->store_id,
				'status' => $this->status,
            ])->with(['user' => function ($query) {
                $query->where([
                    'store_id' => $this->store_id
                ])->select('id, nickname,avatar_url');
            }]);


        $count = $query->count();
        $pagination = new Pagination(['totalCount' => $count]);

        $list = $query->limit($pagination->limit)->offset($pagination->offset)->orderBy(['create_time' => SORT_ASC, 'id' => SORT_DESC])->asArray()->all();



        foreach($list as $k => $v){
            $attrs = json_decode($v['attr'],true);
            $name = '';
            foreach($attrs as $v1){
                $name .= $v1['attr_group_name'].':'.$v1['attr_name'].';';
            }
            $list[$k]['attrs'] = $name; 
        }

        return [
            'list'=>$list,
            'pagination'=>$pagination
        ];

    }



    public function save()
    {
        if (!$this->validate()) {
            return $this->getErrorResponse();
        }
        $this->model->name = $this->name;
        $this->model->status = $this->status?$this->status:0;
        $this->model->realname = $this->realname;
        $this->model->tel = $this->tel;
        $this->model->address = $this->address;
        $this->model->review_result = $this->review_result;
        if($this->model->save()){
			if($this->status == 1){
				$form1 = new Purchase();
				$model = Purchase::findOne([
						'user_id' => $this->user_id,
						'store_id' => $this->store->id,
					]);
					
				$form1->model = $model;
				if($model){
					$form1->model->store_id = $this->store->id;
					$form1->model->user_id = $this->user_id;
					$form1->model->name = $this->name;
					$form1->model->realname = $this->realname;
					$form1->model->tel = $this->tel;
					$form1->model->address = $this->address;
					$form1->model->create_time = time();
					$form1->model->review_result = $this->review_result;
					$form1->model->save();
				}else{
					$form1->store_id = $this->store->id;
					$form1->user_id = $this->user_id;
					$form1->name = $this->name;
					$form1->realname = $this->realname;
					$form1->tel = $this->tel;
					$form1->address = $this->address;
					$form1->review_result = $this->review_result;
					$form1->create_time = time();
					$form1->save();
				}
				
				return [
						'code' => 0,
						'msg' => '保存成功'
					];
			}
			
            return [
                'code' => 0,
                'msg' => '保存成功'
            ];
        } else {
            return $this->getErrorResponse($this->model);
        }
    }
}